package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsQuickExit;
import com.jy.wms.service.WmsQuickExitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsQuickExitController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Sat Apr 28 09:08:37 CST 2018
 */
@RestController
@RequestMapping("/wmsquickexit")
@Api(value = "/wmsquickexit", description = "快速出库")
public class  WmsQuickExitController extends BaseController<WmsQuickExitService,WmsQuickExit> {

    @PostMapping(value = "/add")
    @ApiOperation(value = "快速出库 新增快速出库信息",response = JSONObject.class, notes = "新增快速出库信息")
    public @ResponseBody
    JSONObject add(HttpSession session, @RequestBody WmsQuickExit entity){
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setId(UUIDUtils.uuid());
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            baseService.baseInsert(entity,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增快速出库信息成功","",  JSONObject.fromObject(entity).toString());
        }   catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增快速出库信息成功","",  JSONObject.fromObject(entity).toString());
        } catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增快速出库信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增快速出库信息失败", e.getMessage(),"");
        }
    }

    @GetMapping(value = "/getBh")
    @ApiOperation(value = "快速出库 获取快速出库发运编号",response = JSONObject.class, notes = "获取快速出库发运编号")
    public @ResponseBody
    JSONObject getBh(){
        JSONObject json = getJSONObject();
        try {
            json.put("fybh",baseService.getFybh());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @GetMapping(value = "/getXf/{id}/{ryid}")
    @ApiOperation(value = "快速出库 下发捡货任务",response = JSONObject.class, notes = "下发捡货任务")
    public @ResponseBody
    JSONObject getXf(@PathVariable String id,@PathVariable String ryid, HttpSession session){
        JSONObject json = getJSONObject();
        try {
            baseService.getXf(id,ryid,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"下发捡货任务成功","", "");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"下发捡货任务成功","", "");
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"下发捡货任务失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"下发捡货任务失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "快速出库 分页查询",response = JSONObject.class, notes = "分页查询")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject list(@RequestBody WmsQuickExit entity, HttpSession session)  {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz = (List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            Page<WmsQuickExit> list = new Page<WmsQuickExit>();
            entity.setSshzid(hz.get(0).getHzid());
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            String cjsj_start = entity.getCjsj_start();
            String cjsj_end = entity.getCjsj_end();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String start = null;
            String end = null;
            if (cjsj_start != null && cjsj_start != "") {
                long scsj_start = Long.parseLong(cjsj_start);//开始时间
                Date date1 = new Date(scsj_start);
                Calendar c = Calendar.getInstance();
                c.setTime(date1);
                c.set(Calendar.HOUR_OF_DAY, 00);
                c.set(Calendar.MINUTE, 00);
                c.set(Calendar.SECOND, 00);
                cjsj_start = format.format(c.getTime());
                entity.setCjsj_start(cjsj_start);
            }
            if (cjsj_end != null && cjsj_end != "") {
                long scsj_end = Long.parseLong(cjsj_end);//结束时间
                Date date2 = new Date(scsj_end);
                Calendar c = Calendar.getInstance();
                c.setTime(date2);
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                cjsj_end = format.format(c.getTime());
                entity.setCjsj_end(cjsj_end);
            }
            if(hz.size()>0){
                list = baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    //快速导出明细报表操作

    @PostMapping(value = "exportDetail")
    @ApiOperation(value = "出库明细表 ", response = JSONObject.class, notes = "出库明细表")
    public @ResponseBody
    JsonResult exportDetail(@RequestBody Map map, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws ServiceException, IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String start = null;
        String end = null;
        if (map.get("cjsj_start") != null) {
            String cjsj_start =  map.get("cjsj_start").toString();
            long scsj_start = Long.parseLong(cjsj_start);//开始时间
            Date date1 = new Date(scsj_start);
            start = format.format(date1);
        }
        if (map.get("cjsj_end") != null) {
            String cjsj_end = map.get("cjsj_end").toString();
            long scsj_end = Long.parseLong(cjsj_end);//结束时间
            Date date2 = new Date(scsj_end);
            end = format.format(date2);
        }
        map.put("cjsj_start", start);
        map.put("cjsj_end", end);

        // 订单类型
       /* String fylx = (String) map.get("fylx");
        if (fylx != null &&  !"".equals(fylx)) {
            map.put("fylx", fylx);
        }

        //货品编号
        String hpbh = (String) map.get("hpbh");
        if (hpbh != null && !"".equals(hpbh)) {
            map.put("hpbh", hpbh);
        }

        //货品名称
        String hpmc = (String) map.get("hpmc");
        if (hpmc != null && !"".equals(hpmc)) {
            map.put("hpmc", hpmc);
        }

        //发运状态
        String fyzt = (String) map.get("fyzt");
        if (fyzt != null && !"".equals(fyzt)) {
            map.put("fyzt", fyzt);
        }
        //省
        String sheng = (String) map.get("sheng");
        if (sheng != null && sheng != "") {
            map.put("sheng", sheng);
        }
        //发运编号
        String fybh = (String) map.get("fybh");
        if (fybh != null && fybh != "") {
            map.put("fybh", fybh);
        }*/

        String filename = "出库明细报表";
        //获取当前操作用户的货主id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        map.put("ckid", ckid);
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid=hz.get(0).getHzid();
        map.put("sshzid", hzid);
        List<String> ids = (List) map.get("ids");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("出库明细报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsQuickExit> list = null;

        list = baseService.exportDetail(ids, map);

        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsQuickExit.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }
}
